/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package adacom;

/**
 *
 * @author STATHIS
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Main 
{
  private static String[] Input_Files; 
  private static String Output_File;
  private static int counter=0,common_counter=0;
   
  public Main()
  {
      ArrayList<String> words=new ArrayList();
      boolean status=false;
      while(!status)
      {
       try{
        System.out.print("\nPlease enter the number of input files : ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        counter=Integer.parseInt(br.readLine());
        Input_Files=new String[counter];status=true;}
        catch(IOException | NumberFormatException e){System.out.println("Please provide a valid number of input files!!!");status=false;}
      }
      status=false;
      while(!status)
      {
        Read_Parameters();
        status=Compare_Words(words);
        System.out.println("\n");
      }
      System.out.println("Sorting comparison results....");
      Sorter sort=new Sorter(words);
      System.out.println("Writing sorted results to output file....");
      status=Write_Output(words);
      if(status){System.out.println("Program finished Successfully!!!");System.exit(1);}
      else{System.out.println("Program will terminate !!!!");System.exit(-1);}
   }
        

 
  public static void Read_Parameters()
    {
      try{
       BufferedReader br=null;
       for(int i=0;i<counter;i++)
       {
         System.out.print("Please enter the path of the "+(i+1)+" input file : ");
         br = new BufferedReader(new InputStreamReader(System.in)); 
         Input_Files[i]=br.readLine();
       }
       System.out.print("Please enter the path of the output file : ");
       Output_File=br.readLine();
      }
      catch(IOException io)
      {
          System.out.println("Unexpected error in reading programme parameters : \n" +io.toString());
          System.out.println("Program will terminate !!!");System.exit(-1);
      }
    }
    
    
  public static boolean Compare_Words(ArrayList words)
   {
     try
     {
       FileReader f1,f2; 
       String s1; String s2;
       f1=new FileReader(Input_Files[0]);
       BufferedReader br1 = new BufferedReader(f1); 
     
	while((s1 = br1.readLine()) != null) 
        {
          for(int i=1; i<counter; i++)
          {
           f2=new FileReader(Input_Files[i]);
           BufferedReader br2 = new BufferedReader(f2); 
           while((s2=br2.readLine())!=null)
           {
            if(s1.equals(s2)&& s1.length()!=0)
             {common_counter=common_counter+1;break;}
           }
           br2.close();f2.close();
          }
          if(common_counter==counter-1){words.add(s1);}
          common_counter=0;
        }
        br1.close();f1.close();
       return true;
     }
     catch(IOException io)
     {System.out.println("\nA problem occured reading from input files!!!\n"+io.toString());}
     return false;
    }
    
  public static boolean Write_Output(ArrayList words)
   {
     try
     {
       FileWriter fw;File f=new File(Output_File); 
       if(!f.exists())
       {f.createNewFile();}
       fw=new FileWriter(f);
       for(int i=0;i<words.size();i++)
       {fw.write(words.get(i).toString()+System.getProperty("line.separator"));}
       fw.close();
     }
     catch(IOException io){System.out.println("A problem occured while writing to output file!!!\n"+io.toString());}
     return true;
   
   
   }
 
  public static void main(String[] args)
  {Main m=new Main();}
}
